package com.china08.yxyapi.api.manage.tec.teaching.lecture.quesgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes.Knows;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 备授课--添加题组
 * 
 */
@CrossOrigin
@RestController
public class LectureQuesCrtController extends YxyApiBaseController {

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @ApiOperation(value = "添加题组")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/quegroup/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String userNick = getProfile(request, response).getUserNick();
        List<String> queStrings = new ArrayList<String>();

        QuesOrder quesOrder = reqModel.quesOrder;
        List<QuesModel> sinQuesList = quesOrder.sinQuesList;
        if (sinQuesList.size() > 0) {
            for (QuesModel quesModel : sinQuesList) {
                queStrings.add(quesModel.quesId);
            }
        }

        List<QuesModel> mulQuesList = quesOrder.mulQuesList;
        if (mulQuesList.size() > 0) {
            for (QuesModel quesModel : mulQuesList) {
                queStrings.add(quesModel.quesId);
            }
        }

        List<QuesModel> gapQuesList = quesOrder.gapQuesList;
        if (gapQuesList.size() > 0) {
            for (QuesModel quesModel : gapQuesList) {
                queStrings.add(quesModel.quesId);
            }
        }

        List<QuesModel> ansQuesList = quesOrder.ansQuesList;
        if (ansQuesList.size() > 0) {
            for (QuesModel quesModel : ansQuesList) {
                queStrings.add(quesModel.quesId);
            }
        }
        String resName = reqModel.resName;
        Map<String, List<Knows>> map = new HashMap<>();
        List<QuesIdKnows> quesIdKnows = reqModel.quesIdKnows;
        List<Knows> list = null;
        List<String> listIds = null;
        Knows knows = null;
        YxyQuesKnow know = null;
        for (QuesIdKnows quesIdKnows2 : quesIdKnows) {
            listIds = quesIdKnows2.know;
            list = new ArrayList<>();
            for (String string : listIds) {
                know = yxyQuesKnowRepository.findById(Integer.valueOf(string));
                knows = new Knows();
                knows.know_id = know.getId().toString();
                knows.know_name = know.getName();
                list.add(knows);
            }
            map.put(quesIdKnows2.id, list);
        }
        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");
        String id = UUID.randomUUID().toString().replace("-", "");
        YxyLectureCatalog yxyLectureCatalog = yxyLectureCatalogRepository.findById(reqModel.catalogId);
        YxyLectureRes yxyLectureRes = new YxyLectureRes();
        yxyLectureRes.setResourceId(id);
        yxyLectureRes.setUserId(userId);
        yxyLectureRes.setSchoolId(schoolId);
        yxyLectureRes.setPhaseId(yxyLectureCatalog.getPhaseId());
        yxyLectureRes.setPhaseName(yxyLectureCatalog.getPhaseName());
        yxyLectureRes.setSubjectId(yxyLectureCatalog.getSubjectId());
        yxyLectureRes.setSubjectName(yxyLectureCatalog.getSubjectName());
        yxyLectureRes.setVersionId(yxyLectureCatalog.getVersionId());
        yxyLectureRes.setVersionName(yxyLectureCatalog.getVersionName());
        yxyLectureRes.setSemesterId(yxyLectureCatalog.getSemesterId());
        yxyLectureRes.setSemesterName(yxyLectureCatalog.getSemesterName());
        yxyLectureRes.setResName(resName);
        yxyLectureRes.setBookcontentsId(reqModel.bookcontentsId);
        yxyLectureRes.setIsDelete(0);
        yxyLectureRes.setStatus(1);
        yxyLectureRes.setFormatId("FMT06");
        yxyLectureRes.setResType(2);
        yxyLectureRes.setQuesKnows(map);
        yxyLectureRes.setQuesIds(queStrings);
        int size = queStrings.size();
        // 默认一道试题为12~15kb
        int number = (int) ((15 - 12) * Math.random() + 12);
        Integer fileSize = size * number * 1024;
        yxyLectureRes.setFileSize(fileSize);
        yxyLectureRes.setResUploader(userNick);
        yxyLectureRes.setTypeId("jl04");
        yxyLectureResRepository.save(yxyLectureRes);
        respModel = new RespModel();
        respModel.setMessage("备授课题组添加成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String catalogId;

        /**
         * 目录id
         */
        public String bookcontentsId;

        /**
         * 题组名称
         */
        public String resName;
        /**
         * 试题ID
         */
        public List<String> quesIds;

        public QuesOrder quesOrder;

        // 试题ID和对应章节知识点交集
        public List<QuesIdKnows> quesIdKnows;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(catalogId, null), "学段id不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "请填写学科");
            Assert.notNull(StringUtils.defaultIfBlank(resName, null), "题组名称不能为空");
        }

    }

    public static class QuesModel {

        // 题id
        public String quesId;

        // 题干
        public Object quesBody;

        // 题型难度
        public String difficultyLevel;

        // 试题涉及的知识点
        public List<String> knowledgeTexts;

        public String questionType;

    }

    public static class QuesOrder {
        // 多选题
        public List<QuesModel> mulQuesList;
        // 单选题
        public List<QuesModel> sinQuesList;
        // 填空题
        public List<QuesModel> gapQuesList;

        // 解答题
        public List<QuesModel> ansQuesList;
    }

    protected static class QuesIdKnows {
        // 试题ID
        public String id;

        // 试题知识点和对应目录知识点交集
        public List<String> know;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
